package main

import (
	"fmt"
	"sync"
	"time"
)

// 编写一个程序，使用多个goroutine并发下载多个文件，并在主goroutine中显示每个文件的下载进度。
// 要求：
// ● 使用多个goroutine并发下载文件
// ● 使用channel传递下载进度信息
// ● 主goroutine显示每个文件的下载进度

func main() {
	strs := []string{"a", "b", "c", "d", "e", "f", "g", "h"}
	wg := sync.WaitGroup{}

	for _, str := range strs {
		wg.Add(1)
		ch := make(chan int)
		go download(str, ch, &wg)
		go func(ch chan int, str string, wg *sync.WaitGroup) {
			for progress := range ch {
				fmt.Println(str, "is downloading:", progress, "/100")
			}
			fmt.Println(str, "is downloaded")
		}(ch, str, &wg)
	}

	wg.Wait()

}

func download(str string, ch chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	defer close(ch)
	for i := 0; i < 101; i++ {
		ch <- i
		if str == "a" {
			time.Sleep(100 * time.Millisecond)
		} else {
			time.Sleep(time.Second)
		}
	}
}
